# 调用长度模型和字符分布模型即时检测参数
from dataAccess import getData
from model import charModel
from model import lenModel
import datetime
import json
FILE_PATH = "C:\\Users\\LENOVO\\Desktop\\machineLearning\\Code\\testSite\\myDetector\\detectorTools\\model\\records\\"
ifReadWithTrainingSets = False
def setIfReadWithTrainingSets(value):
	global ifReadWithTrainingSets
	ifReadWithTrainingSets = value

def cmSerializeHelper(m):
	# 帮助完成模型序列化操作
	return{
		"trainingset":m.getTrainingSet(),
		"sampleToPredict":m.getPredictResult(),
		"realRst":m.getRealResult(),
		"predictRst":m.getPredictResult(),
		"strategy":m.getStrategy(),
		"ICDList":m.getICDList(),
		"CDList":m.getCDList(),
		"postiveRate":m.getPositiveRate(),
	}
def cmFromFile(dic):
	# 帮助完成模型反序列化
	# 2018.1.24	 添加loadTrainingSets 读取模型的时候不加载训练数据
	global ifReadWithTrainingSets
	m = charModel.charModel()
	if ifReadWithTrainingSets:
		m.resetTrainingSet(dic["trainingset"])
	m.setPredictResult(dic["sampleToPredict"])
	m.setRealResult(dic["realRst"])
	m.setPredictResult(dic["predictRst"])
	m.setStrategy(dic["strategy"])
	m.setICDList(dic["ICDList"])
	m.setCDList(dic["CDList"])
	m.setPositiveRate(dic["postiveRate"])
	return m
def lmSerializeHelper(m):
	return{
		"trainingset":m.getTrainingSet(),
		"sampleToPredict":m.getPredictResult(),
		"realRst":m.getRealResult(),
		"predictRst":m.getPredictResult(),
		"mean":m.getMean(),
		"var":m.getVar(),
		"stdev":m.getStdev(),
		"formFlag":m._lenModel__formFlag
	}
def lmFromFile(dic):
	# 2018.1.24	 添加loadTrainingSets 读取模型的时候不加载训练数据
	global ifReadWithTrainingSets
	m = lenModel.lenModel()
	if ifReadWithTrainingSets:
		m.resetTrainingSet(dic["trainingset"])
	m.setPredictResult(dic["sampleToPredict"])
	m.setRealResult(dic["realRst"])
	m.setPredictResult(dic["predictRst"])
	m.setMean(dic["mean"])
	m.setVar(dic["var"])
	m.setStdev(dic["stdev"])
	m._lenModel__formFlag = dic["formFlag"]
	return m

def formFile(model,name,default=None):
	# model 应该是长度模型或者字符分布模型
	with open(FILE_PATH+name+".txt","w") as f:
		f.write(json.dumps(model,default=default))
	print("new ",FILE_PATH+name+".txt finished")
def readModelFromFile(name,obj_hook=None):
	with open(FILE_PATH+name+".txt","r") as f:
		return json.loads(f.read(),object_hook=obj_hook)
def offlineTrainingAndSave(fld,site="",modelType="lenModel",baseTable=""):
	model = None
	helper = lmSerializeHelper
	if(baseTable ==""):
		trainingData = getData.getGood("normalparams",fld)
	else:
		trainingData = getData.getGood(baseTable,"value",where="where arg_name='"+fld+"'")
	today = datetime.date.today()
	month = today.month
	day = today.day
	year = str(today.year)
	if month<10:
		month = str(month)
		month ="0"+month
	else:
		month = str(month)

	if day<10:
		day = str(day)
		day = "0"+day
	else:
		day = str(day)

	fileName = site+fld+"_"+year+month+day
	if modelType == "lenModel":
		model = lenModel.lenModel(trainingSet = trainingData)
		fileName += "_lm"
	elif modelType == "charModel":
		model = charModel.charModel(trainingSet = trainingData)
		helper = cmSerializeHelper
		fileName += "_cm"
	model.train()
	formFile(model,name=fileName,default=helper)
def getFileName(fld,formDate,modelType):
	FILE_NAME = ""+fld+"_"+formDate+"_"+modelType
	return FILE_NAME




if __name__ == '__main__':
	columnName = getData.getAllColumnName("normalparams")
	del(columnName[0])
	for x in columnName:
		offlineTrainingAndSave(x)
		offlineTrainingAndSave(x,modelType="charModel")
